<!-- 正文开始 -->
<div class="layui-fluid" id="deviceSearch">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar" lay-filter="deviceSearch">
                <div class="layui-form-item">
                    <label class="layui-label">科室</label>
                    <div class="layui-inline">
                        <select name="deptCode" lay-filter="deviceSearch-deptCode">
                            <option value="" data-default="">所有科室</option>
                        </select>
                    </div>
                    <label class="layui-label">类型</label>
                    <div class="layui-inline">
                        <select name="deviceType" lay-filter="deviceSearch-deviceType">
                        </select>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn btnSearch"><i class="layui-icon">&#xe615;</i>搜索</button>
                        <button class="layui-btn icon-btn btnUpgrade"><i class="layui-icon layui-icon-upload-circle"></i>升级APP</button>
                        <button class="layui-btn icon-btn btnReload"><i class="layui-icon layui-icon-refresh-1"></i>重新加载设备</button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="deviceTable" lay-filter="deviceTable"></table>

        </div>
    </div>
</div>


<!-- 表格在线状态列 -->
<script type="text/html" id="tpl-deviceTable-online">
    <input disabled type="checkbox" lay-filter="tpl-deviceTable-online" value="{{d.id}}" lay-skin="switch" lay-text="在线|离线"
           {{d.registerState==1 ? 'checked' : ''}}/>
</script>


<script>
    layui.use(['table', 'form', 'http', 'tools'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var form = layui.form;
        var http = layui.http;
        var tools = layui.tools;

        var viewId = "#deviceSearch";
        var $view = $(viewId);
        var $deviceType = $view.find("select[name='deviceType']");
        var $deptCode = $view.find("select[name='deptCode']");
        var orderByClause = "deviceNo";

        var fnGetSearchParam = function(){
            var param = {};
            param.orderByClause = orderByClause;
            param.deptCode = $deptCode.val();
            param.deviceType = $deviceType.val();
            return param;
        };

        var fnRender = function(deviceTypeList) {
            var deviceTypeVal = "";
            tools.select.options($deviceType,deviceTypeList,deviceTypeVal);
            tools.select.deptCodeOptions($deptCode);
            form.render('select', 'deviceSearch');
            //渲染表格
            var deviceTableIns = table.render({
                id: 'deviceTable',
                elem: '#deviceTable',
                method: 'post',
                contentType: "application/json",
                url: http.base_server + 'device/liveList',
                where: fnGetSearchParam(),
                page: true,
                cellMinWidth: 100,
                height: 'full-200',
                limits : [10,20,30,40,50],
                limit : 20,
                autoSort: false,
                request: {
                    pageName: 'pageNum',
                    limitName: 'pageSize'
                },
                cols: [[
                    {type: 'numbers'},
                    {type: 'checkbox'},
                    {field: 'deptName', sort: false, title: '科室', width: 100},
                    {field: 'deviceTypeName', sort: false, title: '类型', width: 60},
                    {field: 'deviceNo', sort: true, title: '设备号', width: 150},
                    {field: 'deviceIp', sort: true, title: 'IP地址', width: 100},
                    {field: 'deviceMac', sort: false, title: 'Mac地址', width: 140},
                    {field: 'bedCode', sort: false, title: '绑定床号', width: 80},
                    {field: 'appVersion', sort: false, title: 'APP版本', width: 80},
                    {field: 'lastAppVersion', sort: false, title: '最新APP', width: 80},
                    {field: 'nurseboardIp', sort: false, title: '护理看板IP', width: 100},
                    {field: 'lastUpTime', sort: false, title: '连线时间', width: 150},
                    {field: 'registerState', sort: false, templet: '#tpl-deviceTable-online', title: '在线', width: 70, align: "center"},
                    {field: 'updateTime', sort: false, title: '更新时间'},
                    {field: 'updateByWho', sort: false, title: '更新人'}
                ]],
                done: function(res){
                  if(res && res.msg){
                      http.okMsg(res.msg);
                  }
                },
                error: function (code, msg) {
                    http.fail(code, msg);
                }
            });

            var fnSearch = function () {
                var param = fnGetSearchParam();
                deviceTableIns.reload({where: param,page: {curr: 1}});
            };

            // 搜索按钮点击事件
            $view.find(".btnSearch").click(function () {
                fnSearch();
            });

            var fnGetActionParam = function(){
                var data = table.checkStatus("deviceTable").data;
                var ipList = [];
                if(data && data.length>0) {
                    for (var i = 0; i < data.length; i++) {
                        if (data[i].registerState === 1) {
                            if(data[i].deviceIp) {
                                ipList.push(data[i].deviceIp);
                            }
                        }
                    }
                }
                return {
                    deptCode: $deptCode.val(),
                    deviceType: deviceTypeVal,
                    deviceIp: ipList.join(",")
                }
            };

            $view.find(".btnUpgrade").click(function(){
                if(typeof  $deviceType.val() === 'undefined'){
                    http.failMsg("请指定类型");
                    return;
                }
                var param = fnGetActionParam();
                if(!param){return;}
                http.post('device/upgrade',param,function(res){
                    if(res.ok){
                        http.okMsg(res.msg);
                    }
                })
            });
            $view.find(".btnReload").click(function () {
                var param = fnGetActionParam();
                if(!param){return;}
                http.post('device/reloadData',param,function(res){
                    if(res.ok){
                        http.okMsg(res.msg);
                    }
                })
            });
            form.on("select(deviceSearch-deptCode)",function(data){
                fnSearch();
            });
            form.on("select(deviceSearch-deviceType)",function(data){
                fnSearch();
            });
            table.on("sort(deviceTable)",function(obj){
                orderByClause = (obj.field || '')+ ' ' + (obj.type || '');
                fnSearch();
            })
        };
        http.get("codelist/listItemByName",{listName:'AppType'},function(res){
            if(res.ok){
                fnRender(res.data);
            }
        });

    });

</script>